package com.example.venadaoa.tool.imagechoose;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import com.bumptech.glide.Glide;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.example.venadaoa.R;
import com.example.venadaoa.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/11/26 0026.
 */
public class LocalPictureViewActivity extends BaseActivity
{
    private ArrayList<String> imgUrlList       = null;
    private int                 mDefaultPosition = 0;

    private ViewPager mPictureVp       = null;
    private PicturePagerAdapter mPictureAdapter  = null;
    private LinearLayout guideGroup;
    private RelativeLayout mTopViewRl;
    private boolean             refreshFlag      = false;

    private List<View> guideViewList    = new ArrayList<View>();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_view);

        mTopViewRl = (RelativeLayout) findViewById(R.id.topViewRl);

        mPictureVp = (ViewPager) findViewById(R.id.gallery);

        guideGroup = (LinearLayout) findViewById(R.id.dot_ll);
        imgUrlList = getIntent().getExtras().getStringArrayList("list");
        if (getIntent().getExtras().getBoolean("show"))
        {
            findViewById(R.id.delete_rl).setVisibility(View.VISIBLE);
        }

        mDefaultPosition = getIntent().getExtras().getInt("pos");

        findViewById(R.id.back_rl).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                LocalPictureViewActivity.this.finish();
            }
        });

        findViewById(R.id.delete_rl).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                refreshFlag = true;
                imgUrlList.remove(mPictureVp.getCurrentItem());
                if (imgUrlList.size() == 0)
                {
                    LocalPictureViewActivity.this.finish();
                } else
                {
                    guideGroup.removeAllViews();
                    addGuideView(guideGroup, mDefaultPosition, imgUrlList);
                    mPictureAdapter.notifyDataSetChanged();
                }

            }
        });

        mPictureAdapter = new PicturePagerAdapter();
        mPictureVp.setAdapter(mPictureAdapter);
        mPictureVp.addOnPageChangeListener(new PisturePageChangeListener());
        mPictureVp.setCurrentItem(mDefaultPosition);
        mPictureAdapter.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onPictureClick(PagerAdapter adapter, View view, int position)
            {
                if (mTopViewRl.getVisibility() == View.VISIBLE)
                {
                    mTopViewRl.setVisibility(View.GONE);
                } else
                {
                    mTopViewRl.setVisibility(View.VISIBLE);
                }
            }
        });

        if (imgUrlList.size() > 1)
            addGuideView(guideGroup, mDefaultPosition, imgUrlList);
    }

    private void addGuideView(LinearLayout guideGroup, int startPos, ArrayList<String> imgUrls)
    {
        if (imgUrls != null && imgUrls.size() > 0)
        {
            guideViewList.clear();
            for (int i = 0; i < imgUrls.size(); i++)
            {
                View view = new View(this);
                view.setBackgroundResource(R.drawable.selector_guide_bg);
                view.setSelected(i == startPos ? true : false);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.gudieview_width), getResources().getDimensionPixelSize(
                        R.dimen.gudieview_width));
                layoutParams.setMargins(10, 0, 0, 0);
                guideGroup.addView(view, layoutParams);
                guideViewList.add(view);
            }
        }
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    public class PicturePagerAdapter extends PagerAdapter
    {
        private OnClickListener mListener = null;

        public PicturePagerAdapter()
        {
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            ((ViewPager) container).removeView((View) object);
            // notifyDataSetChanged();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            View itemView = LayoutInflater.from(LocalPictureViewActivity.this).inflate(R.layout.pager_gallery_item, container, false);

            container.addView(itemView);
            final SubsamplingScaleImageView iv = (SubsamplingScaleImageView) itemView.findViewById(R.id.image);
            iv.setMaxScale(10.0f);
            iv.setMinScale(0.5f);
            iv.setTag(position);
            String path = "";
            // 针对图片是否是网络图片进行判断
            if (!TextUtils.isEmpty(imgUrlList.get(position)) && (imgUrlList.get(position).startsWith("http") || imgUrlList.get(position).startsWith("https")))
            {
                path = imgUrlList.get(position);
            } else
            {
                path = "file://" + imgUrlList.get(position);
            }
            iv.setImage(ImageSource.uri(path));

            iv.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    if (mListener != null)
                    {
                        int position = (Integer) v.getTag();
                        mListener.onPictureClick(PicturePagerAdapter.this, v, position);
                    }
                }
            });
            return itemView;
        }

        @Override
        public int getCount()
        {
            return imgUrlList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            return arg0 == arg1;
        }

        public void setOnClickListener(OnClickListener l)
        {
            mListener = l;
        }

        @Override
        public int getItemPosition(Object object)
        {
            return POSITION_NONE;
        }
    }

    private final class PisturePageChangeListener implements ViewPager.OnPageChangeListener
    {
        @Override
        public void onPageScrollStateChanged(int arg0)
        {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {
        }

        @Override
        public void onPageSelected(int position)
        {
            mDefaultPosition = position;
            for (int i = 0; i < guideViewList.size(); i++)
            {
                guideViewList.get(i).setSelected(i == position ? true : false);
            }
        }
    }

    public interface OnClickListener
    {
        public void onPictureClick(PagerAdapter adapter, View view, int position);
    }

    @Override
    public void finish()
    {
        if (refreshFlag)
        {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putStringArrayList("list", imgUrlList);
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
        }
        super.finish();
    }
}
